﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace WindowsGame1
{
    public enum CameraMode
    {
        FirstPerson,
        ThirdPerson
    }

    class Camera
    {
        Vector3 position = Vector3.Zero;
        Vector3 goingto; 
        Vector3 forward = Vector3.UnitZ;
        Vector3 up = Vector3.UnitY;
        Vector3 left = -Vector3.UnitX;
        Actor3d mActor;

        Matrix projection;
        Matrix view;

        public bool readyToexit = false;
 
        public CameraMode mCameraMode; //
        
        public Camera()
        {
            mCameraMode = CameraMode.FirstPerson;
            
        }

        public void setActor(Actor3d actor)
        {
            mActor = actor;
        }

        public void SwitchMode() {
            if (mCameraMode==CameraMode.FirstPerson) {
                mCameraMode=CameraMode.ThirdPerson;
            } else {
                mCameraMode=CameraMode.FirstPerson;
            }
        }

        public void update(int dTime)
        {
            if (mActor!=null)
            {
                if (mCameraMode == CameraMode.ThirdPerson)
                {
                    mActor.direction.Normalize();
                    goingto = mActor.position - mActor.direction * 2;
                    Vector3 move = goingto - position /20;
                    position += move;
                    forward = mActor.direction - position;
                    forward.Normalize();
                    forward *= 5;
                } else {
                    position = mActor.position;
                    forward = mActor.direction;
                }
            }
 //           if (mCameraMode == CameraMode.ThirdPerson)
 //           {

  //          }
        }

        public Vector3 GetPosition()
        {
            return position;
        }

        public Vector3 GetLookAt()
        {
            return forward;
        }

        public void Walk(float z) {
            position += forward * z;
        }

        public void Strafe (float x) {
            position += left;
        }

        public void Fly(float z)
        {
            position += up;
        }

        public void Rotate(float x, float y, float z)
        {
            forward = Vector3.Transform(forward, Matrix.CreateFromYawPitchRoll(x,y,z));
            left = Vector3.Transform(left, Matrix.CreateFromYawPitchRoll(x,y,z));
            up = Vector3.Transform(up, Matrix.CreateFromYawPitchRoll(x, y, z));
        }

        public void Roll(float z)
        {

        }

        public void Pitch(float x)
        {

        }

        public void Yaw(float z)
        {

        }

        // camera.forward = vector3.transform(matrix.createrotationy(0,01);
    }
}
